/*
 * ItemFilterComponent.java
 *
 * Created on September 2, 2006, 4:14 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package org.infoset.component;

import java.net.URI;
import java.util.logging.Logger;
import org.infoset.xml.Document;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.Name;
import org.infoset.xml.XMLException;
import org.infoset.xml.filter.ItemFilter;
import org.infoset.xml.util.DocumentSource;

/**
 *
 * @author alex
 */
public class ItemFilterComponent extends ComponentBase {
   
   static Logger LOG = Logger.getLogger(ItemFilterComponent.class.getName());
   Document staticInput;
   URI staticInputLocation;
   Name inputPortName;
   Name outputPortName;
   ItemFilter filter;
   ItemDestination output;
   /** Creates a new instance of ItemFilterComponent */
   public ItemFilterComponent(Name name,String vendor,String version,ItemFilter filter, Name inputPortName, Name outputPortName) {
      super(name,vendor,version);
      this.filter = filter;
      this.output = null;
      this.inputPortName = inputPortName;
      this.outputPortName = outputPortName;
   }
   public ItemFilterComponent(Name name,String vendor,String version,ItemFilter filter) {
      this(name,vendor,version,filter,ItemFilterComponentDefinition.DOCUMENT_INPUT_PORT,ItemFilterComponentDefinition.RESULT_OUTPUT_PORT);
   }

   public ItemFilter getFilter() {
      return filter;
   }
   
   public void init(Context context) 
      throws XMLException
   {
      super.init(context);
      this.output = null;
   }
   
   public void bindOutputPort(Name port,ItemDestination output)
      throws XMLException
   {
      if (port.equals(outputPortName)) {
         this.output = output;
         filter.attach(output);
      } else {
         throw new XMLException("Unknown output port "+port+" on "+name);
      }
   }
   
   public ItemDestination getInputPort(Name port)
      throws XMLException
   {
      if (port.equals(inputPortName)) {
         return filter;
      } else {
         throw new XMLException("Unknown input port "+port+" on "+name);
      }
   }

   public void onDocumentInput(Name port,Document doc)
      throws XMLException
   {
      if (port.equals(inputPortName)) {
         DocumentSource.generate(doc,false,filter);
      } else {
         throw new XMLException("Unknown input port "+port+" on "+name);
      }
   }
   
   public void bindResource(Name port,Document document)
      throws XMLException
   {
      staticInput = document;
   }
   
   public void bindResource(Name port,URI location)
      throws XMLException
   {
      staticInputLocation = location;
   }
   
   public void onStart() 
      throws XMLException
   {
      if (staticInputLocation!=null) {
         try {
            context.getDocumentLoader().generate(staticInputLocation,filter);
         } catch (java.io.IOException ex) {
            throw new XMLException("Cannot read static location for input port.",ex);
         }
      } else if (staticInput!=null) {
         DocumentSource.generate(staticInput,false,filter);
      }
   }
}
